package YZ.Students.Mier.Dao.impl;

import YZ.Students.Mier.Dao.UserDao;
import YZ.Students.Mier.Tool.utils.druidTool;
import YZ.Students.Mier.entity.UserEntity;
import lombok.SneakyThrows;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @className: UserDaoImpl
 * @author: shuishui
 * @date: 2022/12/02 19:15
 */
public class UserDaoImpl implements UserDao {
    QueryRunner queryRunner = new QueryRunner(druidTool.dataSource);
    @Override
    public Integer insert(UserEntity userEntity) throws Exception {
        String sql = "insert into t_user(username,password,email,type,status,activationCode,createTime) values(?,?,?,?,?,?,?)";
        return queryRunner.update(sql, userEntity.getUsername(), userEntity.getPassword(), userEntity.getEmail(), userEntity.getType(), userEntity.getStatus(), userEntity.getActivationCode(), userEntity.getCreateTime());
    }

    @Override
    public UserEntity selectById(Integer id) throws Exception {
        String sql = "select * from t_user where id=?";
        return queryRunner.query(sql,new BeanHandler<UserEntity>(UserEntity.class),id);
    }

    @Override
    public Integer updateById(UserEntity userEntity) throws Exception {
        String sql = "update t_user set username = ?,email = ?,status = ? where id = ?";
        return queryRunner.update(sql, userEntity.getUsername(), userEntity.getEmail(), userEntity.getStatus(), userEntity.getId());
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        return null;
    }

    @Override
    public List<UserEntity> selectList() throws Exception {
        return null;
    }

    @Override
    public Integer batchRemove(List<Integer> ids) throws Exception {
        return null;
    }

//    public static void main(String[] args) {
//        UserDaoImpl userDao = new UserDaoImpl();
//
//    }
    @Override
    public UserEntity selectByActivationCode(String activationCode) throws Exception {
        String sql = "select * from t_user where activationCode = ?";
        return queryRunner.query(sql, new BeanHandler<UserEntity>(UserEntity.class), activationCode);
    }

    @Override
    public UserEntity selectByUsername(String username) throws Exception {
        String sql = "select * from t_user where username = ?";
        return queryRunner.query(sql, new BeanHandler<UserEntity>(UserEntity.class), username);
    }

    @Override
    public void updateHeaderUrl(Integer id, String filePath) throws Exception {
        String sql = "update t_user set headerUrl = ? where id = ?";
        queryRunner.update(sql, filePath, id);
    }

    @Override
    public Integer changePassword(Integer id, String newPassword) throws SQLException {
        String sql = "update t_user set password = ? where id = ?";
        return queryRunner.update(sql,newPassword,id);
    }
}
